package com.campusify.service.impl;

import com.campusify.compents.PageResult;
import com.campusify.pojo.AddressExample;
import com.campusify.pojo.AddressExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.AddressMapper;
import com.campusify.pojo.Address;
import com.campusify.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	private AddressMapper addressMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Address> findAll() {
		return addressMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Address> page=   (Page<Address>) addressMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Address address) {
		addressMapper.insert(address);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Address address){
		addressMapper.updateByPrimaryKey(address);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Address findOne(Integer addressID){
		return addressMapper.selectByPrimaryKey(addressID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] addressIDs) {
		for(Integer addressID:addressIDs){
			addressMapper.deleteByPrimaryKey(addressID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Address address, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		AddressExample example=new AddressExample();
		Criteria criteria = example.createCriteria();
		
		if(address!=null){			
						if(address.getUsername()!=null && address.getUsername().length()>0){
				criteria.andUsernameLike("%"+address.getUsername()+"%");
			}			if(address.getReceivername()!=null && address.getReceivername().length()>0){
				criteria.andReceivernameLike("%"+address.getReceivername()+"%");
			}			if(address.getReceiverphone()!=null && address.getReceiverphone().length()>0){
				criteria.andReceiverphoneLike("%"+address.getReceiverphone()+"%");
			}			if(address.getAddressdetail()!=null && address.getAddressdetail().length()>0){
				criteria.andAddressdetailLike("%"+address.getAddressdetail()+"%");
			}	
		}
		
		Page<Address> page= (Page<Address>)addressMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
